package com.joker.demo.admin.controller;

import com.joker.demo.admin.common.Result;
import com.joker.demo.admin.service.DataCleanupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据清理控制器
 *
 * @author : feixiang.li
 * @since : 2025-01-27 10:00
 */
@Slf4j
@RestController
@RequestMapping("/api/data-cleanup")
@Tag(name = "数据清理管理", description = "监控数据清理相关接口")
public class DataCleanupController {

    @Resource
    private DataCleanupService dataCleanupService;

    @PostMapping("/environment-monitor")
    @Operation(summary = "清理环境监控数据", description = "清理超过指定天数的环境监控数据")
    public Result<Map<String, Object>> cleanupEnvironmentMonitorData(
            @Parameter(description = "保留天数", example = "10")
            @RequestParam(defaultValue = "10") int days) {
        
        try {
            log.info("手动触发清理环境监控数据，保留天数：{}", days);
            int deletedCount = dataCleanupService.cleanupEnvironmentMonitorData(days);
            
            Map<String, Object> result = new HashMap<>();
            result.put("deletedCount", deletedCount);
            result.put("retentionDays", days);
            result.put("message", "环境监控数据清理完成");
            
            return Result.success(result);
        } catch (Exception e) {
            log.error("清理环境监控数据失败", e);
            return Result.error("清理环境监控数据失败：" + e.getMessage());
        }
    }

    @PostMapping("/thread-pool-monitor")
    @Operation(summary = "清理线程池监控数据", description = "清理超过指定天数的线程池监控数据")
    public Result<Map<String, Object>> cleanupThreadPoolMonitorData(
            @Parameter(description = "保留天数", example = "10")
            @RequestParam(defaultValue = "10") int days) {
        
        try {
            log.info("手动触发清理线程池监控数据，保留天数：{}", days);
            int deletedCount = dataCleanupService.cleanupThreadPoolMonitorData(days);
            
            Map<String, Object> result = new HashMap<>();
            result.put("deletedCount", deletedCount);
            result.put("retentionDays", days);
            result.put("message", "线程池监控数据清理完成");
            
            return Result.success(result);
        } catch (Exception e) {
            log.error("清理线程池监控数据失败", e);
            return Result.error("清理线程池监控数据失败：" + e.getMessage());
        }
    }

    @PostMapping("/all-monitor")
    @Operation(summary = "清理所有监控数据", description = "清理超过指定天数的所有监控数据")
    public Result<Map<String, Object>> cleanupAllMonitorData(
            @Parameter(description = "保留天数", example = "10")
            @RequestParam(defaultValue = "10") int days) {
        
        try {
            log.info("手动触发清理所有监控数据，保留天数：{}", days);
            int deletedCount = dataCleanupService.cleanupAllMonitorData(days);
            
            Map<String, Object> result = new HashMap<>();
            result.put("deletedCount", deletedCount);
            result.put("retentionDays", days);
            result.put("message", "所有监控数据清理完成");
            
            return Result.success(result);
        } catch (Exception e) {
            log.error("清理所有监控数据失败", e);
            return Result.error("清理所有监控数据失败：" + e.getMessage());
        }
    }

    @GetMapping("/status")
    @Operation(summary = "获取数据清理状态", description = "获取当前数据清理配置和状态信息")
    public Result<Map<String, Object>> getCleanupStatus() {
        
        Map<String, Object> status = new HashMap<>();
        status.put("scheduledTask", "每天凌晨2点执行");
        status.put("retentionDays", 10);
        status.put("batchSize", 1000);
        status.put("tables", new String[]{"t_environment_monitor", "t_job_thread_pool_monitor"});
        status.put("description", "自动清理超过10天的监控数据，避免数据量过大");
        
        return Result.success(status);
    }
}
